#include <stdio.h>
#include <stdlib.h>


typedef struct stack {
    int top;
    int total;
    int data[0];
} stack_t;

stack_t *create_stack(int size) {
    stack_t *node;
    int *data;
    node = malloc(size * sizeof(stack_t) + (size * sizeof(int)));
    node->top = -1;
    node->total = size;
    return node;
}

/* 入栈：向栈顶存入数据 */
int push_stack(stack_t *p, int data) {
    if(p->top == p->total - 1) return -1;
    p->top++;
    p->data[p->top] = data;
    return 0;
}

/* 出栈：取出栈顶数据，并从栈中删除 */
int pop_stack(stack_t *p, int *databuf) {
    *databuf = p->data[p->top];
    if(-1 == p->top) return -1;
    p->top--;
    return 0;
}

/* 读取栈顶元素值 */
int get_top_stack(stack_t *p, int *databuf) {
    *databuf = p->data[p->top];
    return 0;
}

void print(stack_t *p) {
    int i;
    printf("栈内数据为：\n");
    for(i = p->top; i > -1; i--) {
        printf("%d ", p->data[i]);
    }
    printf("\n");
}

int is_empty_stack(stack_t *p) {
    return -1 == p->top;
}

int is_full_stack(stack_t *p) {
    return p->total -1 == p->top;
}

int des_stack(stack_t **pp) {
    free(*pp);
    pp = NULL;
    return 0;
}

int clean_stack(stack_t *p) {
    p->top = -1;
    return 0;
}

/*
int main(int argc, const char *argv[])
{
    int *databuf;
    stack_t *p;
    p = create_stack(10);
    push_stack(p, 10);
    push_stack(p, 20);
    push_stack(p, 30);
    print(p);
    get_top_stack(p, databuf);
    printf("栈顶数据为：%d\n", *databuf);
    pop_stack(p, databuf);
    printf("出顶：%d\n", *databuf);
    get_top_stack(p, databuf);
    printf("栈顶数据为：%d\n", *databuf);
    exit(0);
}
*/
